package com.wc.算法提高课.C第三章_图论.差分约束.糖果;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/17 18:47
 * @description https://www.acwing.com/problem/content/1171/
 */
public class Main {
    /**
     * 思路；差分约束<p>
     * 原理：https://www.acwing.com/file_system/file/content/whole/index/content/154827/<p>
     * 根据题意求最小值, 属于求最长路<p>
     * X == 1, A == B => A >= B, A <= B<p>
     * X == 2, A < B  => B >= A + 1<p>
     * X == 3, A >= B => A >= B<p>
     * X == 4, A > B  => A >= B + 1<p>
     * X == 5, A <= B => B >= A<p>
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010, M = N * 3 + 10, INF = 0x3f3f3f3f;
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static long[] dist = new long[N];
    static int[] cnt = new int[N], q = new int[N];
    static boolean[] st = new boolean[N];
    static int n, m, idx = 1;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        while (m-- > 0) {
            int x = sc.nextInt(), a = sc.nextInt(), b = sc.nextInt();
            if (x == 1) {
                add(a, b, 0);
                add(b, a, 0);
            } else if (x == 2) add(a, b, 1);
            else if (x == 3) add(b, a, 0);
            else if (x == 4) add(b, a, 1);
            else add(a, b, 0);
        }
        for (int i = 1; i <= n; i++) add(0, i, 1);
        long res = 0;
        if (spfa()) res = -1;
        else for (int i = 1; i <= n; i++) res += dist[i];
        out.println(res);
        out.flush();
    }

    static boolean spfa() {
        Arrays.fill(dist, 0, n + 1, -INF);
        dist[0] = 0;
        int hh = 0, tt = 0;
        for (int i = 0; i <= n; i++) {
            q[tt++] = i;
            st[i] = true;
        }
        while (hh != tt) {
            int t = q[hh++];
            if (hh == N) hh = 0;
            st[t] = false;
            for (int i = h[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (dist[j] < dist[t] + w[i]) {
                    dist[j] = dist[t] + w[i];
                    cnt[j] = cnt[t] + 1;
                    if (cnt[j] >= n + 1) return true;
                    if (!st[j]){
                        q[tt++] = j;
                        if (tt == N) tt = 0;
                        st[j] = true;
                    }
                }
            }
        }
        return false;
    }

    static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
